package io.renren.modules.jxgkphone.controller;


import io.renren.modules.jxgk.service.EvaluationTimeService;
import io.renren.modules.jxgk.utils.UUidCreater;
import io.renren.modules.jxgkphone.entity.ClassDiary;
import io.renren.modules.jxgkphone.service.ClassDiaryService;
import io.renren.modules.jxgkphone.tempEntity.TempCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/diary")
public class ClassDiaryController {
    @Autowired
    ClassDiaryService classDiaryService;
    @Autowired
    EvaluationTimeService evaluationTimeService;

    public String CurrentDate(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Currentdate=df.format(new Date());
        return Currentdate;
    }

    @RequestMapping("/{date}/{class_academic_year}/{class_academic_term}/{classId}/ClassInfo")
    public List<TempCourse> SelectAllCourse(@PathVariable("class_academic_year")String class_academic_year,
                                            @PathVariable("class_academic_term")String class_academic_term,
                                            @PathVariable("classId")String classId,
                                            @PathVariable("date")String date){

        List<TempCourse> tc=classDiaryService.SelectAllCourse(classId,class_academic_term,class_academic_year);
        List<TempCourse> finalTc=new ArrayList<>();
        for(TempCourse t:tc){
            List<String> teacherList=evaluationTimeService.valid(date);
            for(String teacherId:teacherList){
                if(t.getCourseTeacherId().equals(teacherId))
                    finalTc.add(t);
            }

        }
        return finalTc;
    }

    @RequestMapping("/{cid}/FindDiary")
    public ClassDiary FindDiary(@PathVariable("cid")String cid){
        ClassDiary classDiary=classDiaryService.FindDiary(cid);
        return  classDiary;
    }

    @RequestMapping("/ClassDiary")
    public String UpdateClassDiary(@RequestBody ClassDiary classDiary){
        String courseTime=classDiaryService.CourseTime(classDiary.getCourseId());
        UUidCreater u=new UUidCreater();
        if(u.compare_date(courseTime,CurrentDate())==1){
            return "拒绝访问";
        } else{
            classDiary.setDiaryId(u.getUUID());
            String b=classDiaryService.UpdateDiary(classDiary);
            return b;
        }

    }



    @RequestMapping("/{year}/{month}/{day}/{index}/DiaryInfo")
    public ClassDiary DiaryInfo(@PathVariable("year")String year,
                                @PathVariable("month")String month,
                                @PathVariable("day")String day,
                                @PathVariable("index")String index){
        return classDiaryService.DiaryInfo(index,year,month,day);
    }

    //用于创建一个json
    @RequestMapping("/Test")
    public ClassDiary Test(){
        ClassDiary cd =new ClassDiary();
        cd.setCourseId("00024525bc214c74975679ec158021f1");
        cd.setDiaryReviseTime("2017-11-13-13:08");
        cd.setCourseIndex("1-2");
        cd.setCoursePosition("明德楼");
        cd.setCourseTeacherName("koukou");
        cd.setCourseName("计算机科学");
        cd.setDiaryPostTime("2017-11-12-13:08");
        cd.setArriveTime("2017-11-12-13:00");
        cd.setLeaveTime("2017-11-12-13:45");
        cd.setTeachLength("45");
        cd.setAttendanceState("正常");
        cd.setHomeworkState("已布置作业");
        cd.setTeachContent("内容1234567");
        cd.setAttcheckState("已考勤");
        cd.setDiaryState("自习");
        return cd;
    }
}

